package com.fy.utils;

import com.fy.config.FileConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    /**
     * 压缩
     */

    public static String zip(String fileName, List<String> paths, FileConfig fileConfig) throws Exception {
        QrCodeUtils.mkdirs(fileConfig.getProfileSimple() + "/zip/");
        //要生成的压缩文件
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fileConfig.getProfileSimple() + "/zip/"+fileName+".zip"));
        File[] files = new File[paths.size()];
        byte[] buffer = new byte[1024];
        for (int i = 0; i < paths.size(); i++) {
            files[i] = new File(paths.get(i));
        }
        for (int i = 0; i < files.length; i++) {
            FileInputStream fis = new FileInputStream(files[i]);
            out.putNextEntry(new ZipEntry(files[i].getName()));
            int len;
            // 读入需要下载的文件的内容，打包到zip文件
            while ((len = fis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            fis.close();
        }
        out.close();
        return fileConfig.getPre() + "/zip/"+fileName+".zip";
    }
}
